<template>
    <div class="setpassword">
        <my-header>设置密码</my-header>
        <form>
            <div :class="{active:active==1}">
                <input type="text" placeholder="请输入手机号" v-model="form.mobile" @focus="active=1" @blur="active=''">
                <p style="color:#eb6100" @click="sendCode" v-if="flag">获取验证码</p>
                <p v-else>获取验证码({{num}})</p>
            </div>
            <div :class="{active:active==2}">
                <input type="text" v-model="form.sms_code" placeholder="请输入短信验证码" @focus="active=2" @blur="active=''">
            </div>
            <div :class="{active:active==3}">
                <input type="text" v-model="form.password" placeholder="请输入密码" @focus="active=3" @blur="active=''">
            </div>
            <p class="button" @click="send">提交</p>
        </form>
    </div>
</template>
<script>
import myHeader from "@/components/myHeader.vue"
import { getCode, editpassword } from "@/http/request"
import { Toast } from 'vant';
const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
export default {
    data() {
        return {
            active: "",
            flag: true,
            num:60,
            form: {
                mobile: "",
                sms_code: "",
                password:'',
            }
        }
    },
    methods:{
        async sendCode() {
            if (!reg.test(this.form.mobile)) {
                Toast.fail('手机号格式不正确');
            } else {
                this.flag = false
                let res = await getCode(this.form)
                console.log(res);
                const t = setInterval(() => {
                    if (this.num >= 0) {
                        this.num--
                    } else {
                        this.flag = true
                        this.num = 60
                        clearInterval(t)
                    }
                }, 1000)
            }
        },
        async send(){
            let res = await editpassword(this.form)
            console.log(res);
        }
    },
    components: {
        myHeader
    }
}
</script>
<style lang="scss" scoped>
.setpassword {
    .active {
        border-bottom: 1px solid #eb6100;
    }

    height: 100vh;
    font-size: 18px;
    background-color: #f8f8f8;

    .head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 10px;
        height: 40px;
        line-height: 40px;
        background-color: #fff;
    }

    form {
        font-size: 14px;
        margin-top: 50px;
        background-color: #fff;

        div {
            margin: 0 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ccc;

            input {
                border: none;
                height: 40px;
                line-height: 40px;
            }
        }

        .box2 {
            margin: 5px 30px;
            font-size: 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            span {
                color: #999;
            }
        }

        .button {
            margin: 50px 30px;
            width: 310px;
            height: 45px;
            line-height: 45px;
            border-radius: 20px;
            background: url('../assets/theme-img/login-btn.png') no-repeat 100%;
            border-bottom: none;
            text-align: center;
            color: #fff;
        }
    }
}
</style>


